import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Merchant } from './merchant.schema'
import { Model } from 'mongoose'

@Injectable()
export class MerchantService {

    // 模型注入
    constructor(@InjectModel(Merchant.name) private MerchantSchema: Model<Merchant>) {
    }
    // 获取商家信息
    async getMerchantInfo(searchList: any[]) {
        let data
        if(searchList.length > 0){
            data = await this.MerchantSchema.find({ $and: searchList }) // 使用

        }else{
            data = await this.MerchantSchema.find() // 使用
        }
        
        return data
    }

    // 添加商家信息
    async addMerchantInfo(data: any) {
        await this.MerchantSchema.create(data)
        return '添加成功'
    }
    //删除商家信息
    async deleteMerchantInfo(data: any) {
        await this.MerchantSchema.deleteOne({ _id: data._id })
        return '删除成功'
    }

    // 修改商家信息
    async updateMerchantInfo(data: any) {
        await this.MerchantSchema.updateOne({ _id: data._id }, data)
        return '修改成功'
    }
}